package com.wanxi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.DeviceVO;
import com.wanxi.dao.DeviceDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.DeviceEntity;
import com.wanxi.service.DeviceService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 14:30
 */
@Service("deviceService")
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    DeviceDao deviceDao;
    @Autowired
    DeviceEntity deviceEntity;

    Jedis jedis = new Jedis();
    public ResultDTO findById(DeviceVO deviceVO) {
//        DeviceEntity deviceEntity = new DeviceEntity();
        BeanUtils.copyProperties(deviceVO,deviceEntity);
//        List<UserEntity> byId = userDao.findById(userEntity);
        DeviceEntity byId = deviceDao.findById(deviceEntity);
        return ResultDTO.getResult(byId);

    }
    public ResultDTO update(DeviceVO deviceVO) {
//        DeviceEntity deviceEntity = new DeviceEntity();
        BeanUtils.copyProperties(deviceVO,deviceEntity);
        int update = deviceDao.update(deviceEntity);
        jedis.del("deviceList");
        return ResultDTO.getResult(update);
    }
    public ResultDTO add(DeviceVO deviceVO) {
//        DeviceEntity deviceEntity = new DeviceEntity();
        BeanUtils.copyProperties(deviceVO,deviceEntity);
        if (deviceEntity.getNormalPrice() !=null){
            deviceEntity.setMarketPrice(deviceEntity.getNormalPrice().add(new BigDecimal(100)));
        }
        int count = deviceDao.add(deviceEntity);
        jedis.del("deviceList");
        return ResultDTO.getResult(count);
    }
    public ResultDTO findAll(DeviceVO deviceVO) {
//        DeviceEntity deviceEntity = new DeviceEntity();
        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(deviceVO,deviceEntity);
        int count = deviceDao.count(deviceEntity);
        List<DeviceEntity> list = new ArrayList<>();

        list = deviceDao.findAll(deviceEntity);
        try {
            String deviceList = mapper.writeValueAsString(list);
            jedis.set("deviceList",deviceList);
            if (deviceEntity.getPage() !=null && deviceEntity.getPage()==1 && jedis.exists("deviceList")){
                String jedisDeviceList = jedis.get("deviceList");
                JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, DeviceEntity.class);
                try {
                    list = mapper.readValue(jedisDeviceList, javaType);
                    jedis.del("deviceList");
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


        return ResultDTO.getResult("", count, list);
    }
    public ResultDTO delete(DeviceVO deviceVO) {
//        DeviceEntity deviceEntity = new DeviceEntity();
        BeanUtils.copyProperties(deviceVO,deviceEntity);
        int delete = deviceDao.delete(deviceEntity);
        jedis.del("deviceList");
        return ResultDTO.getResult(delete);
    }


}
